﻿using System.Diagnostics;

namespace VkontakteSharp.Model
{
    using System;
    using Newtonsoft.Json.Linq;

    /// <summary>
    /// http://vk.com/pages?oid=-1&p=%D0%9E%D0%BF%D0%B8%D1%81%D0%B0%D0%BD%D0%B8%D0%B5_%D0%BF%D0%BE%D0%BB%D1%8F_attachments
    /// </summary>
    public abstract class Attachment
    {
        public static Attachment GetAttachment(JObject att)
        {
            Attachment output;
            switch (att["type"].Value<string>())
            {
                case "audio":
                    output = (Audio)Mapper.Map<Audio>(att["audio"].ToString());
                    break;

                case "photo":
                    output = (Photo)Mapper.Map<Photo>(att["photo"].ToString());
                    break;

                case "posted_photo":
                    output = (Photo)Mapper.Map<Photo>(att["posted_photo"].ToString());
                    break;

                case "video":
                    output = (Video)Mapper.Map<Video>(att["video"].ToString());
                    break;

                case "doc":
                    output = (Document)Mapper.Map<Document>(att["doc"].ToString());
                    break;

                case "graffiti":
                    output = (Graffiti)Mapper.Map<Graffiti>(att["graffiti"].ToString());
                    break;

                case "link":
                    output = (Link)Mapper.Map<Link>(att["link"].ToString());
                    break;

                case "note":
                    output = (Note)Mapper.Map<Note>(att["note"].ToString());
                    break;

                case "app":
                    output = (App)Mapper.Map<App>(att["app"].ToString());
                    break;

                case "poll":
                    output = (Poll)Mapper.Map<Poll>(att["poll"].ToString());
                    break;

                case "page":
                    output = (Page)Mapper.Map<Page>(att["page"].ToString());
                    break;

                default:
                    output = new Link() { Description = att["type"].Value<string>() };
                    Debug.WriteLine("The type of attachment is not defined." + att["type"].Value<string>());
                    //throw new ArgumentException("The type of attachment is not defined.", "Attachment.type");
                    break;
            }

            return output;
        }

//        public static Message GetMessage(JObject current)
//        {
//            var msg = new Message();
//            msg.Id = (long?)current["mid"];
//            msg.UserId = (long?)current["uid"];
//            msg.Title = (string)current["title"];
//            msg.Body = (string)current["body"];
//            msg.ChatId = (long?)current["chat_id"];
//            msg.UsersCount = (int?)current["users_count"];
//            msg.AdminId = (long?)current["admin_id"];
//            msg.FromUserId = (long?)current["from_id"];

//            if (current["deleted"] != null)
//                msg.IsDeleted = (int)current["deleted"] == 1;

//            if (current["date"] != null)
//                msg.Date = UnixTimeStampToDateTime((long)current["date"]);

//            if (current["read_state"] != null)
//            {
//                int state = (int)current["read_state"];
//                switch (state)
//                {
//                    case 0:
//                        msg.ReadState = MessageReadState.Unreaded;
//                        break;

//                    case 1:
//                        msg.ReadState = MessageReadState.Readed;
//                        break;
//                }
//            }

//            if (current["out"] != null)
//            {
//                int type = (int)current["out"];
//                switch (type)
//                {
//                    case 0:
//                        msg.Type = MessageType.Recived;
//                        break;

//                    case 1:
//                        msg.Type = MessageType.Sended;
//                        break;
//                }
//            }

//            if (current["attachments"] != null)
//            {
//                var arr = (JArray)current["attachments"];
//                msg.Attachments = arr.Select(attach => GetAttachment((JObject)attach)).ToList();
//            }

//            if (current["fwd_messages"] != null)
//                throw new NotImplementedException();

//            if (current["chat_active"] != null)
//                throw new NotImplementedException();

//            return msg;
//        }

//        public static Chat GetChat(JObject el)
//        {
//            var chat = new Chat();
//            chat.Id = (long?)el["chat_id"];
//            chat.Title = (string)el["title"];
//            if (el["users"] != null)
//            {
//#if WINDOWS_PHONE
//                chat.UserIds = JArrayToIEnumerable((JArray)el["users"]);
//#else
//                chat.UserIds = JArrayToIEnumerable<long>((JArray)el["users"]);
//#endif
//            }

//            if (el["admin_id"] != null)
//                chat.AdminId = Convert.ToInt64((string)el["admin_id"]);

//            return chat;
//        }
    }
}